package com.jwthhealth_pub.old.diet.view.fragment;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.diet.presenter.IDietPresneterComple;
import com.jwthhealth_pub.old.diet.view.DietSecActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/12/27.
 */
public class SeasonsFragment extends Fragment {

    public static final int JUMP = 0;
    public static final int TOAS = 1;
    public static final int ERROR = 2;

    @BindView(R.id.season_title_one)
    TextView titleOne;
    @BindView(R.id.btn_season_one)
    RelativeLayout btnSeasonOne;
    @BindView(R.id.season_title_two)
    TextView titleTwo;
    @BindView(R.id.btn_season_two)
    RelativeLayout btnSeasonTwo;
    @BindView(R.id.season_title_thi)
    TextView titleThi;
    @BindView(R.id.btn_season_thi)
    RelativeLayout btnSeasonThi;
    @BindView(R.id.season_title_for)
    TextView titleFor;
    @BindView(R.id.btn_season_for)
    RelativeLayout btnSeasonFor;
    @BindView(R.id.progressbar)
    ProgressBar progressBar;

    private boolean loading;

    public Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.d("SeasonsFragment", "this side receive a msg");
            switch (msg.what) {
                case JUMP:
                    Bundle data = msg.getData();
                    Intent intent = new Intent(getActivity(), DietSecActivity.class);
                    intent.putExtras(data);
                    startActivity(intent);
                    progressbar();
                    loading = false;
                    break;
                case TOAS:
                    Toast.makeText(getActivity(), msg.getData().getString(Constant.DIETDATA), Toast.LENGTH_SHORT).show();
                    loading = false;
                    break;
                case ERROR:
                    Toast.makeText(getActivity(), "网络错误", Toast.LENGTH_SHORT).show();
                    progressbar();
                    break;
                default:break;//踌躇满志 发小
            }
        }
    };
    private IDietPresneterComple iDietPresneterComple;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = null;

        view = inflater.inflate(R.layout.fragment_season, container, false);

        ButterKnife.bind(this, view);

        iDietPresneterComple = new IDietPresneterComple(getActivity());
        iDietPresneterComple.setHandler(mHandler);

        initUi();

        return view;
    }

    private void initUi() {
        Resources resources = getActivity().getResources();
        titleOne.setText(resources.getString(R.string.diet_season_one));
        titleTwo.setText(resources.getString(R.string.diet_season_two));
        titleThi.setText(resources.getString(R.string.diet_season_thi));
        titleFor.setText(resources.getString(R.string.diet_season_for));
    }

    private void progressbar() {
        if (progressBar.getVisibility() == View.VISIBLE) {
            progressBar.setVisibility(View.GONE);
        } else {
            progressBar.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.btn_season_one, R.id.btn_season_two, R.id.btn_season_thi, R.id.btn_season_for})
    public void onClick(View view) {

        if (loading) {
            return;
        }

        switch (view.getId()) {
            case R.id.btn_season_one:
                iDietPresneterComple.requestSecInfo("3", "1");
                break;
            case R.id.btn_season_two:
                iDietPresneterComple.requestSecInfo("3", "2");
                break;
            case R.id.btn_season_thi:
                iDietPresneterComple.requestSecInfo("3", "3");
                break;
            case R.id.btn_season_for:
                iDietPresneterComple.requestSecInfo("3", "4");
                break;
        }

        progressbar();
        loading = true;
    }
}
